import {
	post,
	get,
} from '@/api/request.js'
import {
	getOptions
} from '@/scripts/compatible/pageOptions.js'

const getViewInfoData = async () => {
	// 准备参数
	const options = getOptions()
	const token = uni.getStorageSync("token")
	const userInfo = uni.getStorageSync("userInfo")
	const systemInfo = uni.getSystemInfoSync()
	const params = {
		sign: {
			"User-Token": token,
			id: options.id,
			userid: userInfo.id
		},
		ios: systemInfo.platform,
	}

	// 发起请求
	const res = await post('/v3/show/index', params)
	// console.log(res);
	if (res && res.data.code == -44) {
		// 判定为未登录 去登录页
		uni.navigateTo({
			url: '/pages/login/index'
		})
		return
	} else {
		// 判定为已登录 返回数据
		return res
	}
}

const getViewCommentListData = async () => {
	// 准备参数
	const options = getOptions()
	const params = {
		movid: options.id
	}

	// 发起请求
	const res = await post('/v3/show/commentList', params)
	if (res && res.data.type == 1) {
		return res
	}
}

const elanApi = async () => {
	// 发起请求
	const res = await post('/v3/elan/index')
	if (!res) return
	return res
}

const getQisim = async (urlObj, pirce) => {
	// 直播参数
	const userInfo = uni.getStorageSync('userInfo')
	const systemInfo = uni.getSystemInfoSync()
	const params = {
		id: urlObj.id,
		vid: urlObj.vid,
		pirce,
		userid: userInfo.id,
		ios: systemInfo.platform
	}

	// 发起请求
	const res = await post('/v3/show/qisim', params)
	if (res) {
		return res
	}
}

const getUserSession = async () => {
	const UserToken = uni.getStorageSync('UserToken')
	const params = {
		"User-Token": UserToken
	}
	const res = await post('/v3/login/userSession', params)
	if (!res) return
	if (res.data.code == 200) {
		const login = 'ok'
		uni.setStorageSync('login', login)
		return res
	} else {
		const login = 'no'
		uni.setStorageSync('login', login)
		uni.removeStorageSync('userInfo')
		uni.removeStorageSync('UserToken')
		return
	}
}

module.exports = {
	getViewInfoData,
	getQisim,
	getViewCommentListData,
	elanApi,
	getUserSession,
}